package feng.demo.service;

import feng.demo.dao.HotDao;
import feng.demo.dao.TypeDao;
import feng.demo.entity.Commodity;
import feng.demo.entity.Hot;
import feng.demo.entity.Type;
import feng.demo.entity.Types;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class HotServiveImpl implements HotService{
    @Autowired
    HotDao hotDao;
    @Autowired
    TypeDao typeDao;
    @Override
    public List<Hot> findAll() {
        List<Hot> list = hotDao.findAll();
        return doadds(list);
    }

    @Override
    public Hot findById(Integer hotid) {
        Hot hot = hotDao.findById(hotid);
        return doadd(hot);
    }

    @Override
    public Hot findByTypeid(Integer typeid) {
        Hot hot = hotDao.findByTypeid(typeid);
        return doadd(hot);
    }

    @Override
    public int add(Hot hot) {
        return hotDao.add(hot);
    }

    @Override
    public int del(Integer hotid) {
        return hotDao.del(hotid);
    }

    @Override
    public int update(Hot hot) {
        return hotDao.update(hot);
    }

    @Override
    public List<Hot> refresh() {
        List<Hot> hots =new ArrayList<>();
        List<Types> types = typeDao.findAlls();
        for (Types t:types) {
            Integer a=0;
            for (Commodity c :t.getCommoditiys()) {
                a=a+c.getSales();
            }
            t.getBreed().setNum(a);
        }


        return null;
    }

    private  List<Hot> doadds(List<Hot> list){
        for (Hot h:list) {
            Type type = typeDao.findById(h.getType().getTypeid());
            h.setType(type);
        }
        return list;
    }
    private Hot doadd(Hot h){
        Type type = typeDao.findById(h.getType().getTypeid());
        h.setType(type);
        return h;
    }
}
